<template>
  <div class="shopcart">
    <div class="shopcart-main" @click="toggleShopcartDetail">
      <div class="shopcart-left">
      <span class="icon-outer" :class="{lighten:totalPrice > 0}">
        <div class="icon-inner">
          <span class="icon-shopping_cart"></span>
        </div>
        <span v-show="totalPrice > 0" class="tip">{{totalCount}}</span>
      </span>
        <span class="foodPrice" :class="{lighten:totalPrice > 0}" >￥{{totalPrice}}</span>
        <span class="deliveryPrice">另需配送费￥{{deliveryPrice}}元</span>
      </div>
      <div class="shopcart-right" :class="{lighten:totalPrice >= minPrice}">
        <span @click.stop="purchaseFunc" class="purchase" >{{purchaseDesc}}</span>
      </div>
    </div>
    <transition name="slide">
      <div class="shopcart-detail" ref="detail" v-show="totalCount > 0 && shopcartDetailFlag" >
        <div>
          <div class="title">
            <span class="name">购物车</span>
            <span @click="clearAllFunc" class="clearAll">清空</span>
          </div>
          <ul>
            <li class="shopcart-item" v-for="s_food in selectedFoods">
              <span class="name">{{s_food.name}}</span>
              <div class="cartcontrol-wrapper">
                <cartcontrol :food="s_food"></cartcontrol>
              </div>
              <span class="price">￥{{s_food.price}}</span>
            </li>
          </ul>
        </div>
      </div>
    </transition>
    <transition name="fade">
      <div v-show="totalCount > 0 && shopcartDetailFlag" @click="toggleShopcartDetail" class="backdrop"></div>
    </transition>
  </div>

</template>

<script type="text/ecmascript-6">
  import cartcontrol from '../cartcontrol/cartcontrol.vue'
  import IScroll from 'iscroll'

  export default{
    name:'shopcart',
    props:{
      selectedFoods:{
        type: Array
      },
      minPrice: {
        type: Number
      },
      deliveryPrice: {
        type: Number
      }
    },
    data() {
      return{
        shopcartDetailFlag: false
      }
    },
    computed:{
      totalCount() {
        let total_count = 0;
        this.selectedFoods.forEach((food) => {
          total_count += food.count;
        });
        if(!total_count){
          this.shopcartDetailFlag = false;
        }
        this.$nextTick(() => {
          if(this.scroll){
            this.scroll.refresh();
          }
        });
        return total_count;
      },
      totalPrice() {
        let total = 0;
        this.selectedFoods.forEach((food) => {
          total += food.count * food.price;
        });
        return total;
      },
      purchaseDesc() {
       if (this.totalPrice == 0){
         return `￥${this.minPrice}元起送`;
       }
       else if(this.totalPrice < this.minPrice){
         let gap = this.minPrice - this.totalPrice;
         return `还差${gap}元起送`;
       }
        else if(this.totalPrice >= this.minPrice){
         return '去结算';
       }
      }
    },
    methods:{
      toggleShopcartDetail() {
        if(this.selectedFoods.length > 0){
          this.shopcartDetailFlag = !this.shopcartDetailFlag;
        }
        this.$nextTick(() => {
          if(!this.scroll){
            console.log('!this.scroll');
            this.scroll= new IScroll(this.$refs.detail,{
              click:true
            });
          }
          else{
            console.log('this.scroll');
            this.scroll.refresh();
          }
        });

      },
      purchaseFunc() {
        if( this.totalPrice >= this.minPrice )
        window.alert(`支付${this.totalPrice}元`);
      },
      clearAllFunc() {
        this.selectedFoods.forEach((food) => {
          food.count = 0;
        });
        this.selectedFoods = [];
      }
    },
    components: {
      cartcontrol
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  @import "../../common/mixin";
  .shopcart-main{
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 48px;
    z-index: 100;
    background-color:#141d27 ;
    .shopcart-left{
      flex: 1;
      font-size: 0;
      .icon-outer{
        display: inline-block;
        position: relative;
        top: -15px;
        width: 58px;
        height: 58px;
        padding: 6px;
        margin-left: 12px;
        border-radius: 50%;
        background-color: #141d27;
        box-sizing: border-box;
        &.lighten{
          .icon-inner{
            background-color: #00A0DC;
            .icon-shopping_cart{
              color: #ffffff;
            }
          }
        }
        .icon-inner{
          width: 46px ;
          height: 46px;
          border-radius: 50%;
          background-color: #2B343C;
          text-align: center;
          .icon-shopping_cart{
            font-size: 24px;
            color: rgba(255,255,255,0.4);
            line-height: 46px;
          }
        }
        .tip{
          position: absolute;
          top: 0;
          right: -5px;
          /*width: 48px;*/
          height: 18px;
          border-radius: 12px;
          padding: 0 12px;
          background-color: rgb(240,20,20);
          color: #ffffff;
          font-size: 9px ;
          line-height: 16px;
          font-weight: 700;
          text-align: center;
          box-shadow: 0px 4px 8px 0px rgba(0,0,0,0.4);
        }
      }
      .foodPrice{
        display: inline-block;
        height: 24px;
        padding: 0 12px ;
        font-size: 16px;
        color: rgba(255,255,255,0.4);
        font-weight: 700;
        line-height: 24px;
        border-right: 1px solid rgba(255,255,255,0.1);
        vertical-align: top;
        margin-top: 12px;
        &.lighten{
          color: rgb(255,255,255);
        }
      }
      .deliveryPrice{
        display: inline-block;
        height: 24px;
        padding: 0 12px;
        font-size: 16px;
        font-weight: 400;
        line-height: 24px;
        color: rgba(255,255,255,0.4);
        vertical-align: top;
        margin-top: 12px;
      }

    }
    .shopcart-right{
      flex: 0 0 105px;
      width: 105px;
      padding: 0 8px;
      box-sizing: border-box;
      background-color: #2B333B;
      text-align: center;
      &.lighten{
        background-color: #00B43C;
        .purchase{
          color: #ffffff;
        }
      }
      .purchase{
        display: inline-block;
        font-size: 12px;
        color: rgba(255,255,255,0.4);
        font-weight: 700;
        line-height: 24px;
        margin: 12px 0;
      }
    }
  }
  .shopcart-detail{
    position: fixed;
    bottom: 48px;
    left: 0;
    width: 100%;
    max-height: 280px;
    background-color: #ffffff;
    overflow: hidden;
    /*overflow: auto;*/
    z-index: 20;
    .title{
      height: 40px;
      background-color: #f3f5f7;
      padding: 0 18px;
      @include border-1px(rgba(7,17,27,0.1));
      .name{
        font-size: 14px;
        font-weight: 700;
        color: rgb(7,17,27);
        line-height: 40px;
      }
      .clearAll{
        float: right;
        font-size: 12px;
        line-height: 40px;
        color: rgb(0,160,220);
      }
    }
    .shopcart-item{
      height: 48px;
      margin: 0 18px;
      box-sizing: border-box;
      @include border-1px(rgba(7,17,27,0.1));
      .name{
        font-size: 14px;
        color: rgb(7,17,27);
        line-height: 48px;
      }
      .price{
        float: right;
        font-size: 14px;
        color: rgb(240,20,20);
        font-weight: 700;
        margin: 17px 12px 0 0;
      }
      .cartcontrol-wrapper{
        float: right;
        padding-top: 12px;
      }
    }
  }
  .backdrop{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(7,17,27,0.6);
    z-index: 10;
    /*filter: blur(10px);*/
  }
  .slide-enter{
    transform: translate3d(0,100%,0);
  }
  .slide-enter-active{
    transition: all 0.5s ease;
  }
  .slide-leave-active{
    transform: translate3d(0,100%,0);
    transition: all 0.5s ease;
  }
  .fade-enter{
    opacity: 0;
  }
  .fade-enter-active{
    transition: all 0.5s ease;
  }
  .fade-leave-active{
    opacity: 0;
    transition: all 0.5s ease;
  }
</style>
